//import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Locale;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class GenerateMatrixExcelWriter {
	private RandomNumber random = null;
	private int numberOfFeatures = 0;
	private String filename = null;
	
	public GenerateMatrixExcelWriter(
			String name,
			int features,
			double lambda, 
			double mean, 
			double variance, 
			double degreesOfFreedom,
			int n,
			double p,
			double lambdaExp,
			double alpha,
			double lambdaGamma){
		
		random = new RandomNumber(lambda, mean, variance, degreesOfFreedom, n, p, lambdaExp, alpha, lambdaGamma);
		numberOfFeatures = features;
		filename = name;
	}
	
	public void Generate(){		
		try
	    {			
			WorkbookSettings ws = new WorkbookSettings();
			ws.setLocale(new Locale("en", "EN"));
			WritableWorkbook workbook = Workbook.createWorkbook(new File(filename), ws);
			WritableSheet s = workbook.createSheet("Sheet1", 0);  
			writeDataSheet(s);
			workbook.write();
			workbook.close();      
	    }
	    catch (IOException e)
	    {
	      e.printStackTrace();
	    }
	    catch (WriteException e)
	    {
	      e.printStackTrace();
	    }
	}
	
	private void writeDataSheet(WritableSheet sheet) throws WriteException{
		
		/* Format the Font */
	    WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
	    WritableCellFormat cf = new WritableCellFormat(wf);
	    
	    cf.setWrap(true);
	    
	    Label l = new Label(0,0,"Poisson",cf);
	    sheet.addCell(l);
	    
	    l = new Label(1,0,"Normal",cf);
	    sheet.addCell(l);
	    
	    l = new Label(2,0,"Chi Cuadrada",cf);
	    sheet.addCell(l);
	    
	    l = new Label(3,0,"Uniforme",cf);
	    sheet.addCell(l);
	    
	    l = new Label(4,0,"Binomial",cf);
	    sheet.addCell(l);
	    
	    l = new Label(5,0,"Exponencial",cf);
	    sheet.addCell(l);
	    
	    l = new Label(6,0,"Gamma",cf);
	    sheet.addCell(l);
	    
	    try
	    {
	    	
	    	NumberFormat dp3 = new NumberFormat("###############.####");
	    	
			for(int i = 0; i < numberOfFeatures; i++)
			{
				double poissonFeature = new BigDecimal(random.nextPoisson()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
				//System.out.println("poissonFeature: " + new Double(poissonFeature).toString());
				
				double normalFeature = new BigDecimal(random.nextNormal()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
				//System.out.println("normalFeature: " + new Double(normalFeature).toString());
				
				double chiFeature = new BigDecimal(random.nextChi()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
				//System.out.println("chiFeature: " + new Double(chiFeature).toString());
				
				double uniformFeature = new BigDecimal(random.nextUniform()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
				//System.out.println("uniformFeature: " + new Double(uniformFeature).toString());
				
				double binomialFeature =  new BigDecimal(random.nextBinomial()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
				//System.out.println("binomialFeature: " + new Double(binomialFeature).toString());
				
				double exponentialFeature = new BigDecimal(random.nextExponential()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
				//System.out.println("exponentialFeature: " + new Double(exponentialFeature).toString());
				
				double gammaFeature = new BigDecimal(random.nextGamma()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
				//System.out.println("gammaFeature: " + new Double(gammaFeature).toString());
				
			    WritableCellFormat dp3cell = new WritableCellFormat(dp3);
			    
			    Number n = new Number(0, i+1, poissonFeature, dp3cell);
			    sheet.addCell(n);
			    
			    n = new Number(1, i+1, normalFeature, dp3cell);
			    sheet.addCell(n);
			    
			    n = new Number(2, i+1, chiFeature, dp3cell);
			    sheet.addCell(n);
			    
			    n = new Number(3, i+1, uniformFeature, dp3cell);
			    sheet.addCell(n);
			    
			    n = new Number(4, i+1, binomialFeature, dp3cell);
			    sheet.addCell(n);
			    
			    n = new Number(5, i+1, exponentialFeature, dp3cell);
			    sheet.addCell(n);
			    
			    n = new Number(6, i+1, gammaFeature, dp3cell);
			    sheet.addCell(n);
			    
			    
			}
	    }
	    catch (ArithmeticException e)
	    {
	      e.printStackTrace();
	    }
		
	}

}
